<template>
  <div class="login-account">
    <el-form label-width="60px" :rules="rules" :model="account" ref="formRef">
      <el-form-item label="账号" prop="name">
      <el-input v-model="account.name"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password/>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { ElForm } from "element-plus";
import { defineComponent, reactive, ref} from "vue";
import { rules } from '../config/account-config';
import localCache from '@/utils/cache';
import { useStore } from 'vuex'

export default defineComponent({
  setup() {
    //拿到vuex
    const store = useStore()

    const account = reactive({
      name:localCache.getCache('name') ?? '',
      password:localCache.getCache('password') ?? ''
    })
    const formRef = ref<InstanceType<typeof ElForm>>()
    const loginAction = (isKeepPassword: boolean) => {
      // console.log(1245121)
       formRef.value?.validate((valid) => {
         console.log(valid)
         if(valid){
           //验证通过，执行登录逻辑
           //1.判断是否需要记录密码
          if(isKeepPassword){
            //本地缓存
            localCache.setCache('name',account.name)
            localCache.setCache('password',account.password)
          }else{
            localCache.deleteCache('name')
            localCache.deleteCache('password')
          }
          //2.开始进行登录验证
          store.dispatch('login/accountLoginAction',{...account})
         }
       })
    }

    return {
      account,
      rules,
      loginAction,
      formRef,
    }
  }
})
</script>

<style lang="less" scoped>

</style>
